package com.sgb.kjwl.viewmodel.self;

import androidx.databinding.ObservableField;
import android.util.Base64;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.userInfo.ForgetActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ForgetModel extends BaseViewModel {
    public ObservableField<String> account = new ObservableField<>("");
    public ObservableField<String> pwd = new ObservableField<>("");
    public ObservableField<String> pwdEnsure = new ObservableField<>("");
    public ObservableField<String> code = new ObservableField<>("");
    private ForgetActivity activity;
    private SelfRepertory repertory;

    public ForgetModel(ForgetActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void forgetPwdModel() {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().forgetPassword(account.get(), Base64.encodeToString(pwd.get().getBytes(), Base64.DEFAULT), code.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    MToast.showToast(activity, response.body().getData().toString());
                    //LoginActivity.start(activity);
                    activity.finish();
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

//        repertory.forgetPwd(account.get(), pwd.get(), code.get())
//                .subscribe(new Consumer<BaseEntity>() {
//                    @Override
//                    public void accept(BaseEntity baseEntity) throws Exception {
//                        MToast.showToast(activity, baseEntity.getMsg());
//                        if ("200".equals(baseEntity.getCode())){
//                            activity.finish();
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
//                    }
//                });
//}

    public void getCodeModel() {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getForgetCode(account.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                MToast.showToast(activity, response.body().getMsg());
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

//        repertory.getForgetCode(account.get())
//                .subscribe(new Consumer<BaseEntity>() {
//                    @Override
//                    public void accept(BaseEntity baseEntity) throws Exception {
//                        MToast.showToast(activity, baseEntity.getMsg());
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
//                    }
//                });
    }
}
